<template>
  <div>
    <el-form
      ref="formBizSubject"
      :model="formBizSubject"
      label-width="75px"
      :size="defaultFormItemSize"
      label-position="right"
      @submit.native.prevent
    >
      <filter-box :item-width="350" @search="refreshFormBizSuject(true)" @reset="onReset">
        <el-form-item label="题干" prop="formFilter.subjectTitle">
          <el-input
            class="filter-item"
            v-model="formBizSubject.formFilter.subjectTitle"
            :clearable="true"
            placeholder="题干"
          />
        </el-form-item>
        <el-form-item label="所属人员" prop="formFilter.personnelType">
          <el-select
            class="filter-item"
            v-model="formBizSubject.formFilter.personnelType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizSubject.personnelType.impl.loading"
            @visible-change="formBizSubject.personnelType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizSubject.personnelType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属模块" prop="formFilter.moduleType">
          <el-select
            class="filter-item"
            v-model="formBizSubject.formFilter.moduleType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizSubject.moduleType.impl.loading"
            @visible-change="formBizSubject.moduleType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizSubject.moduleType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="题目类型" prop="formFilter.subjectType">
          <el-select
            class="filter-item"
            v-model="formBizSubject.formFilter.subjectType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizSubject.subjectType.impl.loading"
            @visible-change="formBizSubject.subjectType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizSubject.subjectType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="场景类型" prop="formFilter.sceneType">
          <el-select
            class="filter-item"
            v-model="formBizSubject.formFilter.sceneType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizSubject.sceneType.impl.loading"
            @visible-change="formBizSubject.sceneType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizSubject.sceneType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="题目级别" prop="formFilter.subjectLevel">
          <el-select
            class="filter-item"
            v-model="formBizSubject.formFilter.subjectLevel"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizSubject.subjectLevel.impl.loading"
            @visible-change="formBizSubject.subjectLevel.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizSubject.subjectLevel.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      class="page-table"
      :data="formBizSubject.SysUser.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizSubject.SysUser.impl.onSortChange"
      @refresh="refreshFormBizSuject(true)"
      :seq-config="{
        startIndex:
          (formBizSubject.SysUser.impl.currentPage - 1) * formBizSubject.SysUser.impl.pageSize
      }"
    >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-plus"
        :size="defaultFormItemSize"
        @click="onAddRow(3)"
        >新建考试试题</el-button
      >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-plus"
        :size="defaultFormItemSize"
        @click="onAddRow(2)"
        >新建闯关试题</el-button
      >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-plus"
        :size="defaultFormItemSize"
        @click="onAddRow(5)"
        >新建评价项</el-button
      >
      <el-button
        slot="operator"
        type="primary"
        :size="defaultFormItemSize"
        plain
        @click="showImport(3)"
        >导入考试试题</el-button
      >
      <el-button
        slot="operator"
        type="primary"
        :size="defaultFormItemSize"
        plain
        @click="showImport(2)"
        >导入闯关题目</el-button
      >

      <vxe-column title="序号" type="seq" width="50px" />
      <vxe-column title="题目类型" field="subject" />
      <vxe-column title="题目级别" field="level" />
      <vxe-column title="题干" field="subjectTitle" />
      <vxe-column title="分数" field="subjectScore" />
      <vxe-column title="所属人员" field="personnel" />
      <vxe-column title="所属模块" field="module" />
      <vxe-column title="所属场景" field="scene" />
      <vxe-column title="创建时间">
        <template slot-scope="scope">
          <span>{{ formatDateByStatsType(scope.row.createTime, 'day') }}</span>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onEditRow(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onDeleteRow(scope.row)"
          >
            删除
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination
          :total="formBizSubject.SysUser.impl.totalCount"
          :current-page="formBizSubject.SysUser.impl.currentPage"
          :page-size="formBizSubject.SysUser.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizSubject.SysUser.impl.onCurrentPageChange"
          @size-change="formBizSubject.SysUser.impl.onPageSizeChange"
        >
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
import { treeDataTranslate, globalDictTranslate } from '@/utils';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin, cachedPageChildMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import {BizSubjectController, DictionaryController, SystemController} from '@/api';
import editSubject from '@/views/biz/formEditBizSubject';
import importSubject from '@/views/biz/formImportBizSubject';

export default {
  name: 'formBizSubject',
  props: {},
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      deptIdPath: [],
      formBizSubject: {
        formFilter: {
          subjectTitle: '',
          personnelType: null,
          moduleType: null,
          subjectType: null,
          sceneType: null,
          subjectLevel: null
        },
        formFilterCopy: {
          subjectTitle: '',
          personnelType: null,
          moduleType: null,
          subjectType: null,
          sceneType: null,
          subjectLevel: null
        },
        SysUser: {
          impl: new TableWidget(
            this.loadBizSubjectData,
            this.loadBizSubjectVerify,
            true,
            false,
            'createTime',
            1
          )
        },
        isInit: false,
        personnelType: {
          impl: new DropdownWidget(this.loadPersonnelTypeDropdownList)
        },
        moduleType: {
          impl: new DropdownWidget(this.loadModuleTypeDropdownList)
        },
        subjectType: {
          impl: new DropdownWidget(this.loadSubjectTypeDropdownList)
        },
        sceneType: {
          impl: new DropdownWidget(this.loadSceneTypeDropdownList)
        },
        subjectLevel: {
          impl: new DropdownWidget(this.loadSubjectLevelDropdownList)
        }
      }
    };
  },
  methods: {
    onReset() {
      this.$refs.formBizSubject.resetFields();
      this.refreshFormBizSuject(true);
    },
    onAddRow(moduleType) {
      let title = '新建题目';
      switch (moduleType) {
        case 2:
          title = '新建闯关试题';
          break;
        case 3:
          title = '新建考试试题';
          break;
        case 5:
          title = '新建教学评价项';
          break;
      }
      this.$dialog
        .show(
          title,
          editSubject,
          {
            area: '1200px'
          },
          {
            moduleType: moduleType
          }
        )
        .then(res => {
          this.refreshFormBizSuject();
        })
        .catch(() => {});
    },
    onEditRow(row) {
      let title = '编辑题目';
      switch (row.moduleType) {
        case 2:
          title = '编辑闯关试题';
          break;
        case 3:
          title = '编辑考试试题';
          break;
        case 5:
          title = '编辑教学评价项';
          break;
      }
      this.loadRowData(row)
        .then(rowData => {
          this.$dialog
            .show(
              title,
              editSubject,
              {
                area: '1200px'
              },
              {
                rowData: rowData,
                moduleType: rowData.moduleType
              }
            )
            .then(res => {
              this.refreshFormBizSuject();
            })
            .catch(e => {});
        })
        .catch(e => {
          //
        });
    },
    onDeleteRow(row) {
      const params = {
        subjectFilter: {
          subjectId: row.subjectId,
          moduleType: row.moduleType
        }
      };
      this.$confirm('是否删除题目,' + row.subjectTitle + '？')
        .then(res => {
          return BizSubjectController.logicDelete(this, params);
        })
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.formInit();
          } else {
            this.$message.error(res.errorMessage);
          }
        })
        .catch(e => {
          console.error(e);
        });
    },
    loadRowData(row) {
      return new Promise((resolve, reject) => {
        let params = {
          subjectId: row.subjectId
        };

        BizSubjectController.findById(this, params)
          .then(res => {
            resolve(res.data);
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizSubjectData(params) {
      params.subjectFilter = {
        subjectTitle: this.formBizSubject.formFilterCopy.subjectTitle,
        personnelType: this.formBizSubject.formFilterCopy.personnelType,
        moduleType: this.formBizSubject.formFilterCopy.moduleType,
        subjectType: this.formBizSubject.formFilterCopy.subjectType,
        sceneType: this.formBizSubject.formFilterCopy.sceneType,
        subjectLevel: this.formBizSubject.formFilterCopy.subjectLevel
      };
      return new Promise((resolve, reject) => {
        BizSubjectController.list(this, params)
          .then(res => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            });
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizSubjectVerify() {
      this.formBizSubject.formFilterCopy.subjectTitle = this.formBizSubject.formFilter.subjectTitle;
      this.formBizSubject.formFilterCopy.personnelType =
        this.formBizSubject.formFilter.personnelType;
      this.formBizSubject.formFilterCopy.moduleType = this.formBizSubject.formFilter.moduleType;
      this.formBizSubject.formFilterCopy.subjectType = this.formBizSubject.formFilter.subjectType;
      this.formBizSubject.formFilterCopy.sceneType = this.formBizSubject.formFilter.sceneType;
      this.formBizSubject.formFilterCopy.subjectLevel = this.formBizSubject.formFilter.subjectLevel;
      return true;
    },
    refreshFormBizSuject(reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizSubject.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formBizSubject.SysUser.impl.refreshTable();
      }
      this.formBizSubject.isInit = true;
    },
    onResume() {
      this.refreshFormBizSuject();
    },
    initFormData() {},
    formInit() {
      this.initFormData();
      this.refreshFormBizSuject();
    },
    /**
     * 人员类型下拉数据获取函数
     */
    loadPersonnelTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'PersonnelType'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            resolve(res.getList());
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadModuleTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'ModuleType'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            resolve(res.getList());
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadSubjectTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'SubjectType'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            resolve(res.getList());
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadSceneTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'SceneType'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            resolve(res.getList());
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadSubjectLevelDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'SubjectLevel'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            resolve(res.getList());
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    showImport(moduleType) {
      this.$dialog
        .show(
          '导入题目',
          importSubject,
          {
            area: '600px'
          },
          {
            moduleType: moduleType
          }
        )
        .then(res => {
          this.refreshFormBizSuject();
        })
        .catch(e => {});
    }
  },
  created() {
    this.formInit();
  }
};
</script>
